window.onload=function(){
    showTable();
};

function changeAlign(){
    var t = document.getElementById("tbody");
    var x =t.getElementsByTagName("td");
    for (var i = 0; i<x.length; i++) {
        x[i].style = "text-align : center; vertical-align : middle";
    }
}
var jsonAll ;
var json=[] ;
var pageset=7;
var page=1;
function show(json){
    var tb=document.getElementById("tb");
    var length = tb.tBodies[0].rows.length;
    for(var i=0;i<length;++i){
        tb.tBodies[0].deleteRow(0);//每次删除第一行
    }
    for(var i=0;i<json.length;++i){
        var row = tb.tBodies[0].insertRow();
        row.insertCell().innerHTML="<input type='checkbox'/>";
        row.insertCell().innerHTML=json[i].accountNo;
        row.insertCell().innerText=json[i].password;
        row.insertCell().innerText=json[i].custName;
        row.insertCell().innerText=json[i].custPhone;
        row.insertCell().innerText=json[i].sex;
        row.insertCell().innerText=json[i].age;
        row.insertCell().innerText=json[i].consigeeAddr;


        row.insertCell().innerHTML = "<a style='text-decoration:none' class='ml-5' onClick='customer_edit("+json[i].accountNo+")'>"+
            " <i class='Hui-iconfont'>&#xe6df;</i></a>&nbsp;&nbsp;<a style='text-decoration:none' class='ml-5' onclick='customer_del(this,\""+ json[i].accountNo +"\")'" +
            " href='javascript:;' title='删除'>"+
            "<i class='Hui-iconfont'>&#xe6e2;</i></a>";
    }
    changeAlign();
}
/*客户删除*/
function customer_del(obj, id) {
    //alert(id);
    if(confirm('确认要删除吗？')){
        $.ajax({
            url: "/customer!delCustomer",
            type: "post",
            data:{
                "accountNo":id
            },
            // dataType: 'json',
            success: function(data) {
                if(data == "del_success"){
                    layer.msg('已删除!', {
                        icon: 1,
                        time: 1000
                    });
                    window.location.replace(location.href);
                }

            },
            error: function(data) {
                //console.log(data.msg);
            },
        });

    }
}

function showTable(){
    // alert("123");
    var date=null;
    var url="customer!getAllCustomerList";
    sendRequest("post", url, date, ShowAllBack);
}
function ShowAllBack(result){
    var json=JSON.parse(result);
    console.log(json);
    var obj = new Function("return" + json)();
    jsonAll=obj;
    var totalNums = json.length;
    show(jsonAll);
}
function Search(){
    var accountNo= document.getElementById("accountNo").value;
    var custphone= document.getElementById("custphone").value;
    var data="accountNo="+accountNo+"&custPhone="+custphone;
    console.log(data);
    var url="customer!Search";
    sendRequest("post", url, data, ShowAllBack);
}


function customer_edit(accountNo) {
    console.log(accountNo);
    var jsona = [];
    for (var i=0;i<jsonAll.length;i++){
        if(jsonAll[i].accountNo == accountNo){
            jsona.push(jsonAll[i]);
        }
    }
    var jsonb=JSON.stringify(jsona);
    console.log(jsonb);
    sessionStorage.setItem('jsona', jsonb);
    sessionStorage.setItem('jsonAll', jsonAll);
    window.location.href = "customer_edit.html";
}

